<link rel="stylesheet" href="__STATIC_LIB__layuiadmin/layui/layui_ext/dtree/dtree.css">
<link rel="stylesheet" href="__STATIC_LIB__layuiadmin/layui/layui_ext/dtree/font/dtreefont.css">

<form class="layui-form  seller-alone-form" id="userAdd" style="padding:20px;">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>部/院：</label>
            <div class="layui-input-inline">
                <input id="id" type="hidden" name="id" lay-verify="id" value="{$id}">
                <input id="ori_centerName" type="hidden" name="ori_centerName" lay-verify="ori_centerName" value="{$centerName}">
                <input type="text" name="dep_name" lay-verify="dep_name" id="dep_name" class="layui-input" value="{$deptName}" disabled>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>科教中心：</label>
            <div class="layui-input-inline">
                <input type="text" name="sub_center_name" lay-verify="sub_center_name" id="sub_center_name" class="layui-input" value="{$centerName}">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>专业：</label>
            <div class="layui-input-inline">
                <input type="text" name="specialty_name" lay-verify="specialty_name" id="specialty_name" class="layui-input" value="{$specialtyName}" disabled>
            </div>
        </div>
    </div> 
    
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button id="subcenter_edit_save" class="layui-btn" lay-submit="" lay-filter="save">保存</button>
        </div>
    </div>
    {:zlserverToken()}
</form>
<script src="__STATIC_LIB__layuiadmin/layui/layui.js"></script>
<script>
    layui.extend({
        dtree: '__STATIC_LIB__layuiadmin/layui/layui_ext/dtree/dtree'
    }).use(['form', 'laydate', 'dtree'], function () { 
        var form = layui.form, laydate = layui.laydate, dtree = layui.dtree;
        form.render();
 
        //保存用户
        form.on('submit(save)', function(data){
            var id = data.field.id;
            var dep_name = data.field.dep_name;
            var specialty_name = data.field.specialty_name;
            var new_centerName = data.field.sub_center_name;
            var ori_centerName = data.field.ori_centerName;
            
            if(!new_centerName){
                layer.msg('科教中心不能为空！', {time: 1300});
                return false;
            }
            // 科教中心的新名称与原名称相同！
            if(new_centerName == ori_centerName){
                layer.open({
                    type: 1
                    ,offset: 'auto'
                    ,id: 'layerDemoAuto'
                    ,content: '<div style="padding: 20px 100px;">保存成功</div>'
                    ,btn: '关闭'
                    ,btnAlign: 'c' //按钮居中
                    ,shade: 0.3 //显示遮罩
                    ,yes: function(){
                        parent.layui.admin.events.changeTabAndRefresh('/manage/book_draw/subcenter.html', 'subcenterTable');
                    },close: function() {
                        console.log('close');
                    },end: function() {
                        parent.layui.admin.events.changeTabAndRefresh('/manage/book_draw/subcenter.html', 'subcenterTable');
                    }
                });
                return false;
            }

            $("#subcenter_edit_save").addClass("layui-btn-disabled");
            $('#subcenter_edit_save').attr("disabled", true);

            $.ajax({
                type: 'post',
                url: '{:url("BookDraw/subcenteredit")}',
                data: data.field,
                dataType: 'json',
                success: function (e) { 
                    if(e.status === true){ 
                        layer.open({
                            type: 1
                            ,offset: 'auto'
                            ,id: 'layerDemoAuto'
                            ,content: '<div style="padding: 20px 100px;">'+ e.msg +'</div>'
                            ,btn: '关闭'
                            ,btnAlign: 'c' //按钮居中
                            ,shade: 0.3 //显示遮罩
                            ,yes: function(){
                                parent.layui.admin.events.changeTabAndRefresh('/manage/book_draw/subcenter.html', 'subcenterTable');
                            },close: function() {
                                console.log('close');
                            },end: function() {
                                parent.layui.admin.events.changeTabAndRefresh('/manage/book_draw/subcenter.html', 'subcenterTable');
                            }
                        });
                    }else{
                        $("#subcenter_edit_save").removeClass("layui-btn-disabled");
                        $('#subcenter_edit_save').removeAttr("disabled");

                        layer.msg(e.msg, {time: 1300});
                    }
                }
            });
            return false;
        });
    
        layui.use('form', function () {
        // 联动部门
            form.on('select(dep_id)', function (data) {
                getSpecialty(data);
            });
        });
 
        // 得到专业
        function getSpecialty(data) {
            $.ajax({
                type: 'get',
                url: "{:url('BookCommon/getSpecicalties')}",
                data: 'bmdm=' + data.value,
                success: function(e) {
                    if(e.status) {
                        $("#specialty_id").empty();//清空下拉框的值
                        $('#specialty_id').append(new Option('', ''));// 下拉菜单里添加元素
                        
                        $("#class_id").empty();//清空下拉框的值
                        $('#class_id').append(new Option('', ''));// 下拉菜单里添加元素
                        $.each(e.data.data, function (index, item) {
                            $('#specialty_id').append(new Option(item.zymc, item.zydm));// 下拉菜单里添加元素
                        });
                        layui.form.render("select");//重新渲染 固定写法
                    } else {
                        layer.msg(e.msg);
                    }
                }
            });
        };

    });
</script>
